<template>
	<ge-page id="ge-page" :vanNavBarObj="vanNavBarObj">
		<van-search :value="searchValue" shape="round" @search="search" placeholder="搜索" />
		<van-tabs line-width="80rpx" @change="onTabsChange">
			<van-tab title="全部" name="all"></van-tab>
			<van-tab title="出售中" name="on_sale"></van-tab>
			<van-tab title="已下架" name="off_sale"></van-tab>
			<van-tab title="已售罄" name="sold_out"></van-tab>
		</van-tabs>
		<view class="data-box">
			<scroll-view scroll-y="true" style="height: calc(100vh - 400rpx);"  @scrolltolower="onMore">
				<view class="cont-one" v-for="item in list">
					<view class="product-info">
						<image class="product-img" :src="item.goods_image" mode=""></image>
						<view class="info-right">
							<view class="info-name">{{item.goods_name}}</view>
							<view class="info-all">
								<span>销量：{{item.sales_actual}}</span>
								<span>库存：{{item.stock_total}}</span>
							</view>
							<view class="info-all">
								<span>状态：{{item.status}}</span>
								<span>添加时间：{{item.create_time}}</span>
							</view>
							<view class="price">
								￥{{item.goods_price_min}}
							</view>
						</view>
					</view>
					<view class="button-list">
						<view class="button-one" @click="handleEdit(item)">
							编辑
						</view>
						<view class="button-one remove-bg" v-if="item.status == 20" @click="onUpdateStatus(item,true)">
							上架
						</view>
						<view class="button-one remove-bg" v-if="item.status == 10" @click="onUpdateStatus(item,false)">
							下架
						</view>
						<view class="button-one remove-bg" @click="onRemove(item)">
							删除
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="fixed-create" @click="jumpCreate">+</view>
		</view>
	</ge-page>
</template>

<script>
	import {
		payStatusEnum,
		orderStatus
	} from '@/constant/constant';
	export default {
		data() {
			return {
				vanNavBarObj: {
					title: '商品管理',
					hideLeftArrow: true
				},
				searchValue: '',
				query: {
					page: 1
				},
				list: [],
				listType: 'all',
				isMore: false,
			}
		},
		created() {
			this.fetchGoods();
		},
		filters: {

		},
		methods: {
			search(e) {
				this.searchValue = e.detail;
				this.query = {
					searchValue: this.searchValue
				};
				this.list = [];
				this.fetchGoods();
			},
			onTabsChange(e) {
				if (this.listType === e.detail.name) return
				// 初始化
				this.listType = e.detail.name;
				this.searchValue = '';
				this.list = [];
				this.query = {
					page: 1
				};
				this.fetchGoods();
			},
			onMore() {
				if (this.isMore) {
					const page = this.query.page || 1;
					this.query = {
						...this.query,
						page: page + 1
					}
					this.fetchGoods();
				}
			},
			fetchGoods() {
				const query = {
					...this.query,
					goodsName: this.query.searchValue,
					categoryId: 0, // 默认商品分类'全部'
					page: this.query.page || 1,
				}
				uni.api.getGoods({
					listType: this.listType,
					...query
				}).then(res => {
					if (res.list.current_page === res.list.last_page) {
						this.isMore = false;
					} else {
						this.isMore = true;
					}
					this.list = [...this.list, ...(res.list.data || [])];
				})
			},
			handleEdit(record) {
				uni.navigateTo({
					url: `/pages/product/info/index?goods_id=${record.goods_id}`
				})
			},
			onRemove(record) {
				uni.showModal({
					title: '确定该商品删除',
					success: (res) => {
						if (res.confirm) {
							uni.api.removeGoods({
								goodsIds: [record.goods_id]
							}).then(({
								err
							}) => {
								if (!err) {
									this.list = this.list.filter(d => d.goods_id != record.goods_id);
								}
							})
						}
					}
				})
			},
			// 上下架
			onUpdateStatus(record, state) {
				const goods_id = record.goods_id;
				uni.api.goodsState({
					goodsIds: [goods_id],
					state
				}).then(res => {
					uni.api.goodsDetail({
						goods_id
					}).then(res => {
						const goodsInfo = res.goodsInfo;
						this.list = this.list.map(goods => {
							if (goods.goods_id === goodsInfo.goods_id) {
								return goodsInfo
							} else {
								return goods
							}
						})

					})
				})
			},
			jumpCreate(){
				uni.navigateTo({
					url:'/pages/product/info/index'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.data-box {

		.cont-one {
			margin: 20rpx auto 0;
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 20rpx;
			width: 700rpx;

			.product-info {
				display: flex;

				.product-img {
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}

				.info-right {

					.info-all {
						font-size: 22rpx;
						color: #999;
						margin: 5rpx 0;

						&>span {
							margin-right: 8rpx;
						}
					}

					.price {
						color: orange;
						font-size: 28rpx;
					}
				}
			}

			.button-list {
				display: flex;
				justify-content: flex-end;

				.button-one {
					width: 120rpx;
					height: 50rpx;
					background-color: blue;
					color: #fff;
					font-size: 22rpx;
					border-radius: 25rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-left: 20rpx;
				}

				.remove-bg {
					background-color: transparent;
					border: 1rpx solid #666;
					color: #000;
				}
			}
		}
	}

	.fixed-create {
		position: fixed;
		right: 20rpx;
		bottom: 20vh;
		width: 70rpx;
		height: 70rpx;
		border-radius: 50%;
		background: rgba(0, 0, 0, 0.3);
		color:#fff;
		text-align: center;
		font-size: 50rpx;
		line-height: 65rpx;
	}
</style>